package hms.inventory;

import hms.master.MasterController;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.DefaultListModel;

/**
 * Vendors is a collection of vendors
 * 
 * 
 * @author Akilah McIntyre
 * 
 */
public class Vendors extends DefaultListModel {
	public Vendor invVendor;

	/**
	 * // TODO [Akilah] Add Comments
	 */
	public Vendors() {
	}

	/**
	 * Initialize vendors from database
	 */
	public void initVendors() {
		int vendor_id = -1; // unique identifier
		String vendor_name = ""; // name
		String size_count = "select count(*) from VENDOR where VENDOR_id =99 and VENDOR_NAME='UNDEFINED'";
        int undfCount = 0;
        try {
        	ResultSet rsCount = MasterController.getDatabaseObject().getResult(size_count);
        	while(rsCount.next()){
        		undfCount = rsCount.getInt(1);
        		if(undfCount == 0){
        			MasterController.getDatabaseObject().updateCommand("INSERT INTO VENDOR(VENDOR_ID,VENDOR_NAME) VALUES(99,'UNDEFINED')");
        		}
        	}
    		// get data from database associated with dao
		ResultSet rsVendors = MasterController.getDatabaseObject().getResult(
				"SELECT VENDOR_ID, VENDOR_NAME FROM VENDOR WHERE VENDOR_ID !=99 ORDER BY VENDOR_ID");
			while (rsVendors.next()) {
				vendor_id = rsVendors.getInt("VENDOR_ID");
				vendor_name = rsVendors.getString("VENDOR_NAME");
				Vendor vendor_item = new Vendor(vendor_id, vendor_name); // Create
																			// Vendor
																			// instance
																			// from
				this.addElement(vendor_item); // add to vendors model
			}
		} catch (SQLException e) {
			// TODO [Akilah] Write to view or popup dialog
			e.printStackTrace();
		}
	}

	/**
	 * Add vendor
	 * 
	 * @param next_vendor_id
	 * @param new_vendor_name
	 */
	public void addVendor(int next_vendor_id, String new_vendor_name) {
		this.addElement(new Vendor(next_vendor_id, new_vendor_name));
	}

	/**
	 * Add vendor
	 * 
	 * @param new_vendor_name
	 */
	public boolean addDbVendor(String new_vendor_name) {
		boolean isSuccess = false;
		// Get new id as new id may not be +1
		int next_vendor_id = MasterController.getDatabaseObject()
				.addDBTableValueParam("VENDOR", "VENDOR_NAME", "VENDOR_ID",
						new_vendor_name);
		if (next_vendor_id > -1) { // if the insertion was successful
			this.addElement(new Vendor(next_vendor_id, new_vendor_name));
			isSuccess = true;
		}

		return isSuccess;
	}

	/**
	 * @param vendor_name
	 *            New Name of Vendor
	 * @param selIndex
	 *            Index of target vendor
	 * @return if database update is success
	 */
	public boolean updateDbVendor(String vendor_name, int selIndex) {
		boolean isSuccess = false;
		int vendor_id = -1;
		try {
			vendor_id = ((Vendor) this.get(selIndex)).vendor_id;
			isSuccess = MasterController.getDatabaseObject()
					.updateDBTableValueParam("VENDOR", "VENDOR_NAME",
							"VENDOR_ID", vendor_name, vendor_id);
			if (isSuccess) {
				this.changeVendorName(vendor_name, selIndex);
			}
		} catch (Exception ex) {
			isSuccess = false;
		}
		return isSuccess;
	}

	/**
	 * @param vendor_name
	 *            New Name of Vendor
	 * @param selIndex
	 *            Index of target vendor
	 */
	public void changeVendorName(String vendor_name, int selIndex) {
		Vendor destVendor = ((Vendor) this.get(selIndex));
		destVendor.vendor_name = vendor_name;
	}
	
	public boolean deleteDbVendor(String vendor_name, int selIndex) {
		boolean isSuccess = false;
		int vendor_id = -1;
		try {
			vendor_id = ((Vendor) this.get(selIndex)).vendor_id;
			MasterController.getDatabaseObject().updateCommand("UPDATE INVENTORY SET VENDOR_ID = 99 WHERE VENDOR_ID = " + vendor_id);
			
			isSuccess = MasterController.getDatabaseObject()
					.updateCommand("DELETE FROM VENDOR WHERE VENDOR_ID = " + vendor_id);
			
			if (isSuccess) {
				this.deleteVendorName(selIndex);
			}
		} catch (Exception ex) {
			isSuccess = false;
		}
		return isSuccess;
	}

	/**
	 * @param selIndex
	 *            Index of target vendor
	 */
	public void deleteVendorName(String vendor_name, int selIndex) {
		this.remove(selIndex);
		
	}
	public void deleteVendorName(int selIndex) {
		this.remove(selIndex);
		
	}
}